/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import djhu.xml.commons.RootElement;
import djhu.xml.commons.dom.ElementTranslator;
import djhu.xml.commons.dom.handler.ElementEditHandler;
import djhu.xml.commons.dom.handler.ElementParserHandler;
import djhu.xml.commons.dom.handler.ElementTempletHandler;
import djhu.xml.commons.dom.handler.ElementValueMapHandler;
import djhu.xml.commons.dom.handler.XmlLoadTtanslateHandler;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.ElementHandler;
import org.dom4j.io.SAXReader;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author wudihua
 */
public class XmlLoadUtil {
    private SAXReader saxReader = new SAXReader(new DocumentFactory());
    private Set<ElementTranslator> translators = new HashSet();

    public XmlLoadUtil() {
        this.saxReader.setDefaultHandler(new XmlLoadTtanslateHandler(this.translators));
    }

    public void addTranslator(ElementTranslator translator) {
        this.translators.add(translator);
    }

    public void addTranslator(String xpath, ElementTranslator translator) {
        this.saxReader.addHandler(xpath, new ElementEditHandler(translator));
    }

    public void setBeans(Object... beans) {
        Object[] var2 = beans;
        int var3 = beans.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            Object bean = var2[var4];
            this.setReadHandler(bean, new ElementParserHandler(bean));
        }

    }

    public void initValueMap(Map<String, Map<String, Object>> MDData, List<Object> tableRowList, List<Object> tableBottomList, List<Object> contentList, Object... beans) {
        Object[] var6 = beans;
        int var7 = beans.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            Object bean = var6[var8];
            this.setReadHandler(bean, new ElementValueMapHandler(bean, MDData, tableRowList, tableBottomList, contentList));
        }

    }

    public void setTemplate(Object... beans) {
        Object[] var2 = beans;
        int var3 = beans.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            Object bean = var2[var4];
            this.setReadHandler(bean, new ElementTempletHandler(bean));
        }

    }

    public Document load(String xmlFilePath) throws DocumentException {
        return this.saxReader.read(xmlFilePath);
    }

    public Document load(File xmlFile) throws DocumentException {
        return this.saxReader.read(xmlFile);
    }

    public Document loadXmlString(String xmlString) throws UnsupportedEncodingException, DocumentException {
        return this.saxReader.read(new ByteArrayInputStream(xmlString.getBytes("utf-8")));
    }

    private void setReadHandler(Object bean, ElementHandler handler) {
        Class beanClass = bean.getClass();
        if (beanClass.isAnnotationPresent(RootElement.class)) {
            RootElement rootElement = (RootElement)beanClass.getAnnotation(RootElement.class);
            String rootPath = rootElement.path();
            this.saxReader.addHandler(rootPath, handler);
        }

    }
}
